﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ERPStore.Web.Extensions;

using CloudSoft.Extensions;

namespace ERPStore.QuoteCart.Repositories
{
	public static class QueryExtensions
	{
		public static IQueryable<Datas.QuoteCart> GetByUserPrincipal(this IQueryable<Datas.QuoteCart> query, ERPStore.Models.UserPrincipal user)
		{
			var customerPersonId = (user != null && user.CurrentUser != null) ? user.CurrentUser.Id : -1;
			var list = from cart in query
					   where (cart.VisitorId == user.VisitorId
									|| cart.CustomerPersonId == customerPersonId)
					   select cart;

			return list;
		}

		public static IQueryable<Datas.QuoteCart> GetByVisitoryId(this IQueryable<Datas.QuoteCart> query, string visitorId)
		{
			var list = from cart in query
					   where (cart.VisitorId == visitorId)
					   select cart;

			return list;
		}

		public static Datas.QuoteCart GetCurrent(this IQueryable<Datas.QuoteCart> query)
		{
			return query.Where(i => i.CartStateId == (int)Models.CartState.CurrentQuote).FirstOrDefault();
		}

		public static Datas.QuoteCart GetByCode(this IQueryable<Datas.QuoteCart> query, string code)
		{
			if (code.IsNullOrTrimmedEmpty())
			{
				return null;
			}
			return query.Where(i => i.Code.ToLower() == code.ToLower()).FirstOrDefault();
		}
	}
}
